package com.ta.stage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ta.stage.entity.Dept;
import com.ta.stage.entity.Device;
import com.ta.stage.entity.vo.DeviceVo;
import com.ta.stage.result.Result;
import com.ta.stage.service.DeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/device/")
@Api(tags={"设备 DeviceController"})
public class DeviceController extends BaseController{

    @Autowired
    DeviceService deviceService;

    @ApiOperation(value = "查询设备列表")
    @RequestMapping(value = "findDevice",method = RequestMethod.GET)
    @ResponseBody
    public Result findDevice(){
        List<Device> deviceslist = deviceService.list(null);
        return Result.ok(deviceslist);
    }

    @ApiOperation(value = "根据id查询设备")
    @GetMapping("getDevice/{id}")
    public Result getDevice(@PathVariable Long id){
        Device device = deviceService.getById(id);
        return Result.ok(device);
    }

    @ApiOperation(value = "新增设备列表")
    @RequestMapping(value = "saveDevice",method = RequestMethod.POST)
    @ResponseBody
    public Result saveDevice(@RequestBody Device device){
        boolean save = deviceService.save(device);
        return save?Result.ok():Result.fail();
    }

    @ApiOperation(value = "逻辑删除设备")
    @DeleteMapping("{id}")
    public Result removeDevice(@PathVariable Long id){
        boolean flag = deviceService.removeById(id);
        return flag?Result.ok():Result.fail();
    }

    @ApiOperation(value = "修改设备设置")
    @PostMapping("updateDevice")
    public Result updateDevice(@RequestBody Device Device){
        boolean flag = deviceService.updateById(Device);
        return flag?Result.ok():Result.fail();
    }

    @ApiOperation(value = "批量删除设备设置")
    @DeleteMapping("batchRemoveDevice")
    public Result batchRemoveDevice(@RequestBody List<Long> idList){
        deviceService.removeByIds(idList);
        return Result.ok();
    }

    @ApiOperation(value = "设备条件查询带分页")
    @PostMapping("findPageDevice/{current}/{limit}")
    public Result findPageDevice(@PathVariable long current,
                               @PathVariable long limit,
                               @RequestBody(required = false) DeviceVo deviceVo){

        //创建page对象，传递当前页，每页记录数
        Page<Device> page = new Page<>(current,limit);

        //构建条件
        QueryWrapper<Device> wrapper = new QueryWrapper<>();
        Integer id = deviceVo.getId();
        String floorName = deviceVo.getFloorName();
        String storey = deviceVo.getStorey();
        String name = deviceVo.getName();
        //条件可能为空
        if (!StringUtils.isEmpty(id)){
            wrapper.eq("id",deviceVo.getId());
        }
        if (!StringUtils.isEmpty(floorName)){
            wrapper.like("floor_name",deviceVo.getFloorName());
        }
        if (!StringUtils.isEmpty(storey)){
            wrapper.like("storey",deviceVo.getStorey());
        }
        if (!StringUtils.isEmpty(name)){
            wrapper.like("name",deviceVo.getName());
        }
        //调用方法实现分页查询
        Page<Device> pageDevice = deviceService.page(page, wrapper);
        return Result.ok(pageDevice);
    }

    @ApiOperation(value = "设备状态锁定与解锁")
    @PutMapping("lockDevice/{id}/{status}")
    public Result lockDevice(@PathVariable Integer id,
                             @PathVariable Integer status) {
        //根据id查询设备
        Device device = deviceService.getById(id);
        //设置状态
        device.setState(status);
        //调用方法
        deviceService.updateById(device);
        return Result.ok();
    }
}

